//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ksyun.media.shortvideo.kit;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.ksyun.media.shortvideo.utils.FileUtils;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
 * Rookie
 * 1939428911@qq.com
 * 2019/2/26 15:30
 */
public class MyKSYMergeKit {
    private static final String a = "MyKSYMergeKit";
    private static final String b = "/newTranscode";
    public static final int INFO_TRANSCODE_UNSUPPORT = 1;
    public static final int INFO_TRANSCODE_STOPBYUSERS = 2;
    public static final int INFO_TRANSCODE_STARTED = 3;
    public static final int ERROR_TRANSCODE_FAILED = -1;
    public static final int INFO_MERGE_FINISH = 100;
    public static final int ERROR_MERGE_FAILED = -100;
    public static final int ERROR_MERGE_EMPTY = 101;
    private MyKSYTranscodeKit c;
    private KSYEasyMergeKit d;
    private List<String> e;
    private Context f;
    private String g;
    private String h;
    private int i;
    private float j;
    private float k = 15.0F;
    private int l = 3;
    private int m = 600000;
    private int n = '뮀';
    private int o = 1;
    private int p = '걄';
    private int q = 1;
    private int r = 1;
    private int s = 2;
    private float t = 3.0F;
    private int u = 2;
    private int v = 480;
    private int w = 480;
    private int x = 24;
    private Handler y;
    private MyKSYMergeKit.OnErrorListener z;
    private MyKSYMergeKit.OnInfoListener A;
    private int B = 0;
    private int C = 1;
    private int D = 2;
    private int E = 3;
    private int F;
    private boolean G;
    private Map<Uri, Boolean> H;

    public MyKSYMergeKit(Context var1) {
        this.F = this.B;
        this.G = false;
        this.f = var1;
        this.e = new LinkedList();
        this.y = new Handler(Looper.getMainLooper());
        this.F = this.C;
    }

    public void start(List<Uri> var1, String var2, String var3, boolean var4) {
        if(var2 == null) {
            throw new IllegalArgumentException("Must set ouputfile");
        } else {
            this.G = var4;
            this.h = var3;
            this.g = var2;
            this.e.clear();
            this.a(var1, 0);
        }
    }

    public void stop() {
        if(this.F == this.D && this.c != null) {
            this.c.stop();
            this.c = null;
        } else if(this.F == this.E && this.d != null) {
            this.d.stop();
            this.d.release();
            this.d = null;
        }

    }

    private void a(final List<Uri> var1, final int var2) {
        if(var2 < var1.size()) {
            if(this.H != null && !((Boolean)this.H.get(var1.get(var2))).booleanValue()) {
                this.e.add(FileUtils.getPath(this.f, (Uri)var1.get(var2)));
                this.a(var1, var2 + 1);
            } else {
                String var3 = FileUtils.getPath(this.f, (Uri)var1.get(var2));
                File var4 = new File(var3);
                String var5 = FileUtils.getMimeType(var4);
                if(!FileUtils.isSupportedMimeType(var5)) {
                    this.a(1, "Transcode: this file do not support(" + var3 + ")");
                    this.a(var1, var2 + 1);
                } else {
                    MyKSYTranscodeKit var6 = new MyKSYTranscodeKit();
                    this.c = var6;
                    var6.setEncodeMethod(this.l);
                    var6.setTargetResolution(this.v, this.w);
                    var6.setAudioSampleRate(this.p);
                    var6.setAudioChannels(this.o);
                    var6.setVideoFps(this.k);
                    var6.setVideoBitrate(this.m);
                    var6.setAudioBitrate(this.n);
                    var6.setVideoCodecId(this.r);
                    var6.setVideoEncodeProfile(this.s);
                    var6.setIFrameInterval(this.t);
                    var6.setVideoCrf(this.x);
                    var6.setVideoDecodeMethod(this.u);
                    var6.setOnInfoListener(new MyKSYTranscodeKit.OnInfoListener() {
                        public void onInfo(MyKSYTranscodeKit var1x, int var2x, String var3) {
                            Log.d("KSYMergeKit", "transcode kit info:" + var2x);
                            if(var2x == 2001) {
                                var1x.release();
                                MyKSYMergeKit.this.e.add(var3);
                                MyKSYMergeKit.this.c = null;

                                try {
                                    MyKSYMergeKit.this.a(var1, var2 + 1);
                                } catch (Exception var5) {
                                    Log.e("KSYMergeKit", "File select error:" + var5);
                                }
                            } else if(var2x == 2002) {
                                MyKSYMergeKit.this.a(MyKSYMergeKit.this.e);
                                MyKSYMergeKit.this.e.clear();
                                MyKSYMergeKit.this.c = null;
                                MyKSYMergeKit.this.a(2, (String)null);
                            } else if(var2x == 100) {
                                MyKSYMergeKit.this.a(3, var3);
                            }

                        }
                    });
                    var6.setOnErrorListener(new MyKSYTranscodeKit.OnErrorListener() {
                        public void onError(MyKSYTranscodeKit var1x, int var2x, long var3) {
                            MyKSYMergeKit.this.a(-1, var2x, var3);
                            var1x.release();
                            MyKSYMergeKit.this.c = null;
                            MyKSYMergeKit.this.a(var1, var2 + 1);
                        }
                    });
                    StringBuilder var7 = new StringBuilder(this.a());
                    var7.append("/newTranscode");
                    var7.append(String.valueOf(var2));
                    var7.append(".mp4");
                    var6.start(FileUtils.getPath(this.f, (Uri)var1.get(var2)), var7.toString());
                    this.i = var2;
                    this.F = this.D;
                }
            }
        } else {
            if(this.e != null && this.e.size() > 0) {
                this.d = new KSYEasyMergeKit();
                this.d.setOnInfoListener(new com.ksyun.media.shortvideo.kit.KSYEasyMergeKit.OnInfoListener() {
                    public void onInfo(KSYEasyMergeKit var1, int var2, String var3) {
                        if(var2 == 101) {
                            var1.release();
                            Log.d("KSYMergeKit", "onInfo: type:100 file:" + MyKSYMergeKit.this.g);
                            MyKSYMergeKit.this.a(100, MyKSYMergeKit.this.g);
                            MyKSYMergeKit.this.a(MyKSYMergeKit.this.e);
                        }

                    }
                });
                this.d.setOnErrorListener(new com.ksyun.media.shortvideo.kit.KSYEasyMergeKit.OnErrorListener() {
                    public void onError(KSYEasyMergeKit var1, int var2, long var3) {
                        var1.release();
                        MyKSYMergeKit.this.a(-100, var2, var3);
                        MyKSYMergeKit.this.a(MyKSYMergeKit.this.e);
                    }
                });
                this.d.start(this.e, this.g);
                this.F = this.E;
            } else {
                this.a(101, 0, 0L);
            }

        }
    }

    public float getTranscodeProgress() {
        if(this.c != null) {
            this.j = this.c.getProgress();
        }

        return this.j;
    }

    public int getCurrentTransFileId() {
        return this.i + 1;
    }

    private String a() {
        String var1 = this.h != null?this.h:Environment.getExternalStorageDirectory().getAbsolutePath() + "/secondAppVideo";
        File var2 = new File(var1);
        if(!var2.exists()) {
            var2.mkdir();
        }

        return var1;
    }

    private void a(List<String> var1) {
        this.F = this.C;
        if(this.G) {
            for(int var2 = 0; var2 < var1.size(); ++var2) {
                File var3 = new File((String)var1.get(var2));
                if(var3.exists() && var3.isFile()) {
                    var3.delete();
                }
            }
        }

    }

    public void release() {
        if(this.y != null) {
            this.y.removeCallbacksAndMessages((Object)null);
            this.y = null;
        }

    }

    public void setTargetSize(int var1, int var2) {
        if(this.F != this.D && this.F != this.E) {
            this.v = var1;
            this.w = var2;
        }
    }

    public void setVideoBitrate(int var1) {
        if(this.F != this.D && this.F != this.E) {
            this.m = var1;
        } else {
            Log.d("KSYMergeKit", " do not support set bitrate during merging");
        }
    }

    public void setVideoKBitrate(int var1) {
        this.setVideoBitrate(var1 * 1024);
    }

    public void setAudioBitrate(int var1) {
        if(this.F != this.D && this.F != this.E) {
            this.n = var1;
        } else {
            Log.d("KSYMergeKit", " do not support set bitrate during merging");
        }
    }

    public void setAudioKBitrate(int var1) {
        this.setAudioBitrate(var1 * 1000);
    }

    public void setAudioChannels(int var1) {
        if(this.F != this.D && this.F != this.E) {
            this.o = var1;
        }
    }

    public void setEncodeMethod(int var1) {
        if(this.F != this.D && this.F != this.E) {
            this.l = var1;
        }
    }

    public void setVideoDecodeMethod(int var1) {
        if(this.F != this.D && this.F != this.E) {
            this.u = var1;
        }
    }

    public void setAudioSampleRate(int var1) {
        if(this.F != this.D && this.F != this.E) {
            this.p = var1;
        }
    }

    public void setVideoFps(float var1) {
        if(this.F != this.D && this.F != this.E) {
            this.k = var1;
        }
    }

    public void setVideoCodecId(int var1) {
        if(var1 != 1 && var1 != 2 && var1 != 3) {
            throw new IllegalArgumentException("input video codecid error");
        } else if(this.F != this.D && this.F != this.E) {
            this.r = var1;
        }
    }

    public void setVideoProfile(int var1) {
        this.s = var1;
    }

    public void setIFrameInterval(float var1) {
        this.t = var1;
    }

    public void setVideoCrf(int var1) {
        this.x = var1;
    }

    public void setOnErrorListener(MyKSYMergeKit.OnErrorListener var1) {
        this.z = var1;
    }

    public void setOnInfoListener(MyKSYMergeKit.OnInfoListener var1) {
        this.A = var1;
    }

    private void a(final int var1, final String var2) {
        if(this.y != null) {
            this.y.post(new Runnable() {
                public void run() {
                    if(MyKSYMergeKit.this.A != null) {
                        MyKSYMergeKit.this.A.onInfo(var1, var2);
                    }

                }
            });
        }

    }

    private void a(final int var1, final int var2, final long var3) {
        if(this.y != null) {
            this.y.post(new Runnable() {
                public void run() {
                    if(MyKSYMergeKit.this.z != null) {
                        MyKSYMergeKit.this.z.onError(var1, var2, var3);
                    }

                }
            });
        }

    }

    public void setToTranscodeFiles(Map<Uri, Boolean> var1) {
        this.H = var1;
    }

    public interface OnErrorListener {
        void onError(int var1, int var2, long var3);
    }

    public interface OnInfoListener {
        void onInfo(int var1, String var2);
    }
}
